<?
    function getInterpolation($xml_file) {
        global $interpol;
        $objeto = new DOMDocument();
        $objeto->load($xml_file);
        $filho = $objeto->getelementsbytagname("interpolation");
        if ($filho->length > 0) {
            $interpol = $filho->item(0)->getelementsbytagname("interpol");
            return $interpol;
        }
        else return null;
    }

    function getPNumber($indice) {
        global $interpol, $p;
        $pnumber = $interpol->item($indice)->getelementsbytagname("num_points");
        if ($pnumber->length > 0) {
            $p = $pnumber->item(0)->nodeValue;
            return $p;
        }
        else return null;
    }

    function getIValue($indice) {
        global $interpol, $value;
        $ivalue = $interpol->item($indice)->getelementsbytagname("interpol_value");
        if ($ivalue->length > 0) {
            $value = $ivalue->item(0)->nodeValue;
            return $value;
        }
        else return null;
    }

    function getFunction($indice) {
        global $interpol, $f;
        $function = $interpol->item($indice)->getelementsbytagname("function");
        if ($function->length > 0) {
            $f = $function->item(0)->nodeValue;
            return $f;
        }
        else return null;
    }

    function getDistribution($indice) {
        global $interpol, $d;
        $options = $interpol->item($indice)->getelementsbytagname("options");
        if ($options->length > 1) {
            $d = $options->item(1)->getattribute("distribution");
            return $d;
        }
        else return null;
    }

    function getData($indice) {
        global $interpol, $data;
        $options = $interpol->item($indice)->getelementsbytagname("options");
        if ($options->length > 0) {
            $data = $options->item(0)->getattribute("data");
            return $data;
        }
        else return null;
    }

    function getPoints($indice) {
        global $interpol, $pontos;
        $points = $interpol->item($indice)->getelementsbytagname("point");
        if ($points->length > 0) {
            for($i = 0; $i < $points->length; $i++) {
                $x = $points->item($i)->getelementsbytagname("x");
                $pontos[2*$i] = $x->item(0)->nodeValue;
                $y = $points->item($i)->getelementsbytagname("y");
                $pontos[2*$i+1] = $y->item(0)->nodeValue;
            }
            return $pontos;
        }
        else return null;
    }

    function getInterval($indice) {
        global $interpol, $intvl;
        $interval = $interpol->item($indice)->getelementsbytagname("interval");
        if ($interval->length > 0) {
            for($i = 0; $i < $interval->length; $i++) {
                $x = $interval->item($i)->getelementsbytagname("x");
                $intvl[2*$i] = $x->item(0)->nodeValue;
                $y = $interval->item($i)->getelementsbytagname("y");
                $intvl[2*$i+1] = $y->item(0)->nodeValue;
            }
            return $intvl;
        }
        else return null;
    }

    function getDesc($indice) {
        global $interpol, $desc;
        $descricao = $interpol->item($indice)->getelementsbytagname("description");
        $desc = $descricao->item(0)->nodeValue;
        return $desc;
    }

    function getCom($indice) {
        global $interpol, $com;
        $comentario = $interpol->item($indice)->getelementsbytagname("comments");
        if ($comentario->length > 0) {
            $com = $comentario->item(0)->nodeValue;
            return $com;
        }
        else return null;
    }

    function getRef($indice) {
        global $interpol, $ref;
        $referencia = $interpol->item($indice)->getelementsbytagname("reference");
        if ($referencia->length > 0) {
            $ref = $referencia->item(0)->nodeValue;
            return $ref;
        }
        else return null;
    }

    function createMenu() {
        global $interpol,$msg;
        for($i = 0; $i < $interpol->length; $i++) {
            echo "<a class=bibliotecaexemplos href=javascript:createLinks($i)><u>";
            $temp = getData($i); $temp1 = $i+1;
            echo $msg->get('XML_EXIBE_INTERPOLACAO_MSG01')."$temp1".$msg->get('XML_EXIBE_INTERPOLACAO_MSG02')." ";
            if ($temp=="discrete") {
                $temp = $msg->get('XML_EXIBE_INTERPOLACAO_MSG03');
            }
            else if ($temp=="continuous") {
                $temp = $msg->get('XML_EXIBE_INTERPOLACAO_MSG04');
            }
            echo "$temp";
            echo "</u></a><p>";
        }
        return;
    }
    if (empty($xml_file) && empty($id_bib)) $id_bib="bib_demo.xml";
    if ( (!isset($indice)) || ($indice == null) ) { ?>

    <p>

        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function createLinks(value) {
               document.myform.indice.value = value;
               document.myform.action = 'index.php?inc=newton.php&modo=exemplo';
               document.myform.submit();
            }

        </script>

	  <? //$document_root = 'c:/adaptweb/adaptweb/html/anaweb';
      ?>


        <? if (isset($id_bib)) $xml_file = $DOCUMENT_ROOT."/bibliotecas/".$id_bib; ?>
        <? verificaBib(@$xml_file); ?>
        <?  if (file_exists($xml_file)) {
                $temp = getInterpolation($xml_file);
                if (!$temp==null) {
                   echo "<p>";
                   echo "<p align=left><font face=Arial>".$msg->get('XML_EXIBE_MSG04')."</font>";
                   echo "<p>";
                   createMenu();
                } else {
                     echo $msg->get('XML_EXIBE_MSG05');
                     echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
                }
	    } else {
                 echo $msg->get('XML_EXIBE_MSG06');
                 echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
	    }
 	 ?>

        <input type="hidden" name="id_bib" value="<? echo $id_bib ?>">
        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="indice">
        <input class="button" type="button" value=<? echo $msg->get('XML_EXIBE_MSG07'); ?> onClick="history.back()">
        </form>

<? } else { ?>
<p>

        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function sendToCalculus() {

               document.myform.action = 'index.php?id_bib=<?echo $id_bib?>&indice=<?echo $indice?>&inc=newton.php&modo=exemplo';
               document.myform.submit();
            }

        </script>

        <? getInterpolation($xml_file); ?>
        <? $dados = getData($indice);?>
        <? $funcao = getFunction($indice);?>
        <? $n = getPNumber($indice);?>
        <? $distribuicao = getDistribution($indice);?>
        <? $valor = getIValue($indice);?>
        <? $points = getPoints($indice);?>
        <? $intvl = getInterval($indice);?>
        <? $a = $intvl[0]; $b = $intvl[1]; ?>
        <? $desc = getDesc($indice); ?>
        <? $com = getCom($indice); ?>
        <? $ref = getRef($indice); ?>
        <? verificaBib(@$xml_file); ?>

        <? if (!strlen($desc) == 0) { ?>
              <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_INTERPOLACAO_MSG05'); ?> </b><? echo htmlentities(utf8_decode($desc)) ?></font>
        <? } ?>
        
        <? if ($dados=="continuous") { ?>
        <input type="hidden" name="opcao_execucao" value="1">
	    <p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_INTERPOLACAO_MSG06'); ?></font>
		<p align="left"><font face="Arial">f(x) = </font>
        <input type="text" size='60' maxlength='100' name="funcao" value="<? echo $funcao ?>">
		<p>
		<p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_INTERPOLACAO_MSG07'); ?></font>&nbsp
        <input type="text" size='5' maxlength='5' name="n" value="<? echo $n ?>">
		<p>
		<p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_INTERPOLACAO_MSG08'); ?><? if ($n>1) echo $n ?> <? echo $msg->get('XML_EXIBE_INTERPOLACAO_MSG09'); ?>
        [ <input type="text" size='5' maxlength='5' name="a" value="<? echo $a ?>"><b>,</b><input type="text" size='5' maxlength='5' name="b" value="<? echo $b ?>">]</font>
		<p>
		<input type="radio" name="distribuicao" value="1" <? echo (($distribuicao==1)or($distribuicao=="uniform"))?"checked":"unchecked"; ?>><font face="Arial"><? echo $msg->get('XML_EXIBE_INTERPOLACAO_MSG010'); ?></font><br>
		<input type="radio" name="distribuicao" value="2" <? echo (($distribuicao==2)or($distribuicao=="chebyshev"))?"checked":"unchecked"; ?>><font face="Arial"><? echo $msg->get('XML_EXIBE_INTERPOLACAO_MSG011'); ?></font> <a href="javascript:void(0)" onClick="window.open('info.php?topico=chebyshev', 'Informacoes', 'toolbar=no, location=no, directories=no, scrollbars=yes,resizable=yes,menubar=no,width=400,height=300')"><img src="imagens/info.gif" border="0"></a><br>
		<br>
		<p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_INTERPOLACAO_MSG012'); ?> </font>
		<input type="text" size='5' maxlength='8' name="valor" value="<? echo $valor ?>">
		<p>
        <? } else if ($dados=="discrete") { ?>
        <input type="hidden" name="Calcular" value="sim">
        <input type="hidden" name="grau" value="<? echo ($n-1) ?>">
        <input type="hidden" name="opcao_execucao" value="0">
        <p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_INTERPOLACAO_MSG07'); ?> </font>
        <input type="hidden" size='5' maxlength='5' name="n" value="<? echo $n ?>"><? echo $n ?>
		<p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_INTERPOLACAO_MSG014'); ?></font>
		<br><br>
		<table>
			<tr>
			   <td align="center"><font face="Arial">x</font></td>
			   <td align="center"><b>,</b></td>
			   <td align="center"><font face="Arial">y</font></td>
			</tr>
			<? for ($i=0; $i<$n; $i++) { ?>
	    		    <tr>
		  	       <td align="center">( <? echo $points[2*$i] ?><input type="hidden" size='5' maxlength='30' name="X[<? echo $i ?>]" value="<? echo $points[2*$i] ?>"></td>
 				 <td align="center"><b>,</b></td>
	    		       <td align="center"> <? echo $points[2*$i+1] ?> <input type="hidden" size='5' maxlength='30' name="Y[<? echo $i ?>]" value="<? echo $points[2*$i+1] ?>"> )</td>
			    </tr>
			<? } ?>
		</table>
		<br>
		<p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_INTERPOLACAO_MSG012'); ?> </font>
		<input type="text" size='5' maxlength='8' name="valor" value="<? echo $valor ?>">
		<p>
		<? } ?>
        <? if (!$com == null) { ?>
              <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MSG08');?></b><? echo htmlentities(utf8_decode($com)) ?></font>
        <? } ?>
        <p>
        <? if (!$ref == null) { ?>
              <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MSG09'); ?> </b><? echo htmlentities(utf8_decode($ref)) ?></font>
        <? } ?>
        <p>

        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="executar" value="sim">
        <input type="hidden" name="navegacao" value="sim">
        <input class="button" type="button" value="Proximo >>" onClick="sendToCalculus()"><p>
        </form>
	<?
	echo "<form name=\"exemplos\" enctype=\"multipart/form-data\" method=\"post\">";
	echo "<script language=\"javascript\" type=\"text/javascript\">\n";
	echo "function criarLinks(value) {\n";
	echo "document.exemplos.indice.value = value;\n";
	echo "document.exemplos.action = 'index.php?inc=newton.php&modo=exemplo'\n";
	echo "document.exemplos.submit();\n";
	echo "}\n";
	echo "</script>";
	echo "<input type=\"hidden\" name=\"id_bib\" value=\"".$id_bib."\">";
	echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
	echo "<input type=\"hidden\" name=\"indice\">";
	echo "</form>";
	echo $msg->get('Outros_Exemplos').":<br>";
    $objeto = new DomDocument();
    $objeto->load($xml_file);
	//$raiz = $objeto->document_element();
	$filho = $objeto->getelementsbytagname("interpolation");
	if ($filho->length > 0)
	{
		$interpol = $filho->item(0)->getelementsbytagname("interpol");
		for ($i=0; $i<$interpol->length; $i++)
		{
			if ($i != 0) echo " | ";
			if ($i == $indice) echo "<b>";
			echo "<a href=\"javascript:criarLinks($i);\"><u>".($i+1)."</u></a>";
			if ($i == $indice) echo "</b>";
		}
	}
	?>

<? } ?>
